package com.kylin;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;

public class ExcelReadTest {

    String path = "C:\\Data\\IntelliJ IDEA Work Space\\kylin-poi\\";

    @Test
    public void testRead03() throws IOException {
        //获取文件流
        FileInputStream fileInputStream = new FileInputStream(path + "Kylin观众统计表03.xls");
        //1.创建一个工作簿
        Workbook workbook = new HSSFWorkbook(fileInputStream);
        //2.创建一个工作表,表中的设置
        Sheet sheet = workbook.getSheetAt(0);//通过下表获取
        //3.得到行
        Row row1 = sheet.getRow(0);
        //4.得到列
        Cell cell11 = row1.getCell(0);//(1,1)
        Cell cell12 = row1.getCell(1);//(1,20
        //得到行列中的数据
        System.out.println(cell11.getStringCellValue());
        System.out.println(cell12.getNumericCellValue());
        fileInputStream.close();

    }

    @Test
    public void testRead07() throws IOException {
        //获取文件流
        FileInputStream fileInputStream = new FileInputStream(path + "Kylin观众统计表07.xlsx");
        //1.创建一个工作簿
        Workbook workbook = new XSSFWorkbook(fileInputStream);
        //2.创建一个工作表,表中的设置
        Sheet sheet = workbook.getSheetAt(0);//通过下表获取
        //3.得到行
        Row row1 = sheet.getRow(0);
        //4.得到列
        Cell cell11 = row1.getCell(0);//(1,1)
        Cell cell12 = row1.getCell(1);//(1,20
        //得到行列中的数据
        System.out.println(cell11.getStringCellValue());
        System.out.println(cell12.getNumericCellValue());
        fileInputStream.close();

    }

    @Test
    public void testCellType() throws IOException {
        //获取文件流
        FileInputStream fileInputStream = new FileInputStream(path + "课题清单.xls");
        //创建一个工作簿
        Workbook workbook = new HSSFWorkbook(fileInputStream);
        //创建一个工作表,表中的设置
        Sheet sheet = workbook.getSheetAt(0);//通过下表获取
        //获得标题内容
        Row rowTitle = sheet.getRow(0);
        if (rowTitle != null) {
            int cellCount = rowTitle.getPhysicalNumberOfCells();//获得列数的总和
            for (int cellNum = 0; cellNum < cellCount; cellNum++) {
                Cell cell = rowTitle.getCell(cellNum);
                if (cell != null) {
                    int cellType = cell.getCellType();//获得单元格类型
                    String cellValue = cell.getStringCellValue();
                    System.out.print(cellValue + " ");
                }
            }
            System.out.println();
        }

        //获取表中的内容
        int rowCount = sheet.getPhysicalNumberOfRows();//获取总共有多少行
        //从第二行开始
        for (int rowNum = 1; rowNum < rowCount; rowNum++) {
            Row rowData = sheet.getRow(rowNum);
            if (rowData != null) {
                int cellCount = rowTitle.getPhysicalNumberOfCells();//获得列数的总和
                for (int cellNum = 0; cellNum < cellCount; cellNum++) {
                    Cell cell = rowData.getCell(cellNum);
                    //匹配列的数据类型
                    if (cell != null) {
                        int cellType = cell.getCellType();
                        String cellValue = "";
                        switch (cellType) {
                            //字符串
                            case HSSFCell.CELL_TYPE_STRING:
                                System.out.print("【String】");
                                cellValue = cell.getStringCellValue();
                                break;
                            //布尔
                            case HSSFCell.CELL_TYPE_BOOLEAN:
                                System.out.print("【BOOLEAN】");
                                cellValue =String.valueOf(cell.getBooleanCellValue());//转为String
                                break;
                            //空
                            case HSSFCell.CELL_TYPE_BLANK:
                                System.out.print("【BLANK】");
                                cellValue = cell.getStringCellValue();
                                break;
                            //数字（日期，普通数字）
                            case HSSFCell.CELL_TYPE_NUMERIC:
                                System.out.print("【NUMERIC】");
                                if (HSSFDateUtil.isCellDateFormatted(cell)){
                                    //日期
                                    System.out.print("【日期】");
                                    Date date = cell.getDateCellValue();
                                    cellValue = new DateTime(date).toString("yyyy-MM-dd");
                                }else{
                                    //普通数字,防止数字过长
                                    System.out.print("【转换为字符串输出】");
                                    cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                                    cellValue = cellValue.toString();
                                }
                                break;
                            //数据类型错误
                            case HSSFCell.CELL_TYPE_ERROR:
                                System.out.print("【数据类型错误】");
                                break;
                        }
                        System.out.println(cellValue);
                    }
                }
            }
        }
       fileInputStream.close();
    }

    @Test
    public void testFormula() throws IOException {
        FileInputStream inputStream = new FileInputStream(path + "公式.xlsx");

        Workbook workbook = new XSSFWorkbook(inputStream);

        //获得表
        Sheet sheet = workbook.getSheetAt(0);

        Row row = sheet.getRow(5);
        Cell cell = row.getCell(0);

        //拿到计算公式 eval
        FormulaEvaluator formulaEvaluator = new XSSFFormulaEvaluator((XSSFWorkbook) workbook);


        //输出单元格的内容
        int cellType = cell.getCellType();
        switch (cellType){
            case Cell.CELL_TYPE_FORMULA://公式
                String formula = cell.getCellFormula();
                System.out.println(formula);

                //计算
                CellValue evaluate = formulaEvaluator.evaluate(cell);
                String cellValue = evaluate.formatAsString();
                System.out.println(cellValue);
                break;
        }



    }
}
